import request from '@/utils/request'
import JSEncrypt from 'jsencrypt'
// import axios from 'axios'

// 登录
export const userLogin = (data) => {
  const publicKey = `-----BEGIN PUBLIC KEY-----
  MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIIBGcCnWAqvPQ+KGeHmQjWWu/1OSgXb
  TyJhIlHAYoQdr6s1pRKnmYd6Z6DCQue/+Sm0qqXPonsXvUFThg8Rvc0CAwEAAQ==
  -----END PUBLIC KEY-----`
  const encrypt = val => {
    const encryptor = new JSEncrypt()
    encryptor.setPublicKey(publicKey) // 设置公钥
    return encryptor.encrypt(val)
  }
  const { vcode, loginName, password } = data
  return request({
    url: '/auth/login',
    method: 'post',
    data: {
      username: loginName,
      password: encrypt(password),
      code: vcode
    }
  })
}
// 获取accesstoken
export const getAccessToken = async (base64) => {
  // const res = await axios({
  //   url: process.env.VUE_APP_BASE_API + ':8058/baiduapi/oauth/2.0/token',
  //   params: {
  //     grant_type: 'client_credentials',
  //     client_id: 'MX8k9bEQi4QRXn2DThCV33x5',
  //     client_secret: 'OBt1ghPOaTSDvLBIbuTgpFfBG455upWz'
  //   }
  // })
  // return axios({
  //   url: process.env.VUE_APP_BASE_API + ':8058/baiduapi/rest/2.0/ocr/v1/idcard',
  //   method: 'post',
  //   params: {
  //     access_token: res.data.access_token
  //   },
  //   headers: {
  //     'Content-Type': 'application/x-www-form-urlencoded'
  //   },
  //   data: {
  //     image: base64,
  //     id_card_side: 'front',
  //     detect_direction: 'true'
  //   }
  // })
  // return axios({
  //   url: process.env.VUE_APP_BASE_API + process.env.VUE_APP_URL + '/ocr/idcard',
  //   method: 'post',
  //   data: {
  //     base64
  //   }
  // })
  return request({
    url: '/ocr/idcard',
    method: 'post',
    data: {
      base64
    }
  })
}

// 获取所有学科
export const subject = () => {
  return request({
    url: '/subject'
  })
}
// 获取班级
export const subjectCls = (params) => {
  const { month, year, subjectId } = params
  let data = {
    subjectId,
    openTime: year + '-' + month
  }
  if (subjectId === 9) {
    data = { subjectId }
  }
  return request({
    url: '/class',
    params: data
  })
}
// 用户新增
export function guestAdd (data) {
  return request({
    url: '/guest',
    method: 'post',
    data
  })
}
// 查询 身份信息
export function checkIdNumber (id) {
  return request({
    // url: '/guest/checkIdNumber/' + id
    url: '/guest/?idNumber=' + id
  })
}
// 获取所有公寓
export const flat = () => {
  return request({
    url: '/flat',
    noError: true
  })
}
// 获取房间列表
export const room = (flatId) => {
  return request({
    url: '/room',
    params: {
      flatId
    }
  })
}

// 入住
export function guestCheckin (guestID, bedID) {
  return request({
    url: '/guest/selectbed',
    params: {
      guestId: guestID,
      bedId: bedID
    }
  })
}
